﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class FormBangYeuCauNguyenLieu : Form
    {
        public FormBangYeuCauNguyenLieu()
        {
            InitializeComponent();
        }

        private void label4_Click(object sender, EventArgs e)
            {

            }

        private void FormBangYeuCauNguyenLieu_Load(object sender, EventArgs e)
            {
            NguyenLieuBUS nl = new NguyenLieuBUS();
            DataTable tb = nl.DSNguyenLieu();
            int numrow = tb.Rows.Count;
            for (int i = 0; i < numrow; i++)
                {
                CmbItem item = new CmbItem();
                item.Text = tb.Rows[i][1].ToString(); ;
                item.Value = tb.Rows[i][0];
                comboBox1.Items.Add(item);
                }
            comboBox1.SelectedIndex = 0;
           
            }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {   NguyenLieuBUS nl = new NguyenLieuBUS();
            DataTable tb = nl.DSNguyenLieu();
            int numrow = tb.Rows.Count;
           

 
            string txt = (comboBox1.SelectedItem as CmbItem).Value.ToString();
            for(int i=0;i<numrow;i++)
                {
                if(tb.Rows[i][0].ToString().Equals(txt))
                    {
                    textBox1.Text = tb.Rows[i][2].ToString();
                    break;
                    }
                }
           
            }

        private void button1_Click(object sender, EventArgs e)
            {
            string id = "001";
           if(dataGridView1.Rows.Count!=1)
                {
                 id= dataGridView1[0,dataGridView1.Rows.Count-2].Value.ToString();
                 int id2 = int.Parse(id);
                 id2++;
                 int length = 3;
                 id = id2.ToString().PadLeft(length, '0');
                }
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dataGridView1);
                row.Cells[0].Value = id;
                row.Cells[1].Value = (comboBox1.SelectedItem as CmbItem).Text.ToString();
                row.Cells[2].Value = textBox1.Text;
                row.Cells[3].Value = textBox2.Text;
                row.Cells[4].Value = textBox4.Text;
                dataGridView1.Rows.Add(row);
                
 
            }
    }
    public class CmbItem
        {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
            {
            return Text;
            }
        }
}
